<script>
var report_result_init = false;
var is_administrator = false
    if ('{{permission}}' == 'true') {
        is_administrator = true;
    }
$(document).ready(function() {
    var initial_width;
    $('#report_table').jqGrid({
        url: '/schedule/job/grid',
        datatype: 'json',
        mtype: 'GET',
        colNames: ["编号", "名称", "统计对象", "对象名称", "开始时间", "结束时间", "间隔/分钟", "创建人", "创建时间", "完成状态", "操作"],
        colModel: [{
            name: 'id',
            index: 'id',
            width: 10,
            align: 'left',
            hidden: true
        }, {
            name: 'name',
            index: 'name',
            width: 15,
            align: 'left',
            formatter: function(cellvalue, option, row) {
                return '<a href="#" onclick="detail_open(\'' + option.gid + '\',\'' + option.rowId + '\')">' + cellvalue + '</a>';
            },
            unformat: function(cellvalue) {
                return cellvalue;
            }
        }, {
            name: 'report_type',
            index: 'report_type',
            width: 15,
            align: 'left'
        }, {
            name: 'type_name',
            index: 'type_name',
            width: 15,
            align: 'left'
        }, {
            name: 'start_time',
            index: 'start_time',
            width: 20,
            align: 'left'
        }, {
            name: 'end_time',
            index: 'end_time',
            width: 20,
            align: 'left'
        }, {
            name: 'interval_time',
            index: 'interval_time',
            width: 10,
            align: 'left'
        }, {
            name: 'owner',
            index: 'owner',
            width: 10,
            align: 'left'
        }, {
            name: 'created_at',
            index: 'created_at',
            width: 20,
            align: 'left'
        }, {
            name: 'status',
            index: 'status',
            width: 15,
            align: 'left'
        }, {
            name: 'operation',
            index: 'operation',
            width: 20,
            align: 'left',
            detailHidden: true,
            formatter: function(cell, option, row) {
                var html = '';
                html += '<button title="删除" class="btn btn-blue" onclick=job_delete(' + row[0] + ',"' + row[1] + '")>删除</button>';
                return html;
            }
        }],
        page: 1,
        rowNum: 10,
        sortname: 'id',
        sortorder: 'desc',
        viewrecords: true,
        multiselect: is_administrator,
        multiboxonly: true,
        multiselectWidth: '60px',
        autowidth: true,
        beforeSelectRow: function(rowid, e) {
            if ($(e.target).is('input')) {
                return true;
            }
            return false;
        },
        gridComplete: function() {
            var gridData = $(this).jqGrid('getRowData');
            jqgrid_page($(this), gridData);
            jqgrid_resize($(this));
        }
    });

    $("#report_create_button").click(function() {
        angular.element('#report_create_button').scope().open("schedule/job/create");
    });

    $("#all_report_button").click(function() {
        angular.element('#all_report_button').scope().open("schedule/result");
    });
    $(".title-tag ul li").on('click', function() { 
        $(this).siblings().removeClass('active');
        $(this).addClass('active');
        var tid = $(this).attr('tid');
        if (tid == '#job_list') {
            $('#job_result').hide();
            $("#job_list").show();
        } else {
            $("#job_list").hide();
            $('#job_result').show();
            initResultTable();
        }
    });

});

function initResultTable() {
    if (!report_result_init) {
        $('#report_all_table').jqGrid({
            url: '/schedule/report/grid',
            datatype: 'json',
            mtype: 'GET',
            colNames: ["编号", "名称", "所有者", "生成时间", "操作"],
            colModel: [{
                name: 'id',
                index: 'id',
                width: 10,
                align: 'left',
                hidden: true
            }, {
                name: 'name',
                index: 'name',
                width: 15,
                align: 'left',
                formatter: function(cellvalue, option, row) {
                    return '<a href="#" onclick="detail_open(\'' + option.gid + '\',\'' + option.rowId + '\')">' + cellvalue + '</a>';
                },
                unformat: function(cellvalue) {
                    return cellvalue;
                }
            }, {
                name: 'owner',
                index: 'owner',
                width: 15,
                align: 'left'
            }, {
                name: 'interval_time',
                index: 'interval_time',
                width: 15,
                align: 'left'
            }, {
                name: 'operation',
                index: 'operation',
                width: 25,
                align: 'left',
                detailHidden: true,
                formatter: function(cell, option, row) {
                    var html = '';
                    row = row2object(row, option.gid);
                    return '<button title="下载" class="btn btn-blue" onclick=confirm_report_load(' + row['id'] + ')>下载<button style="margin-left:10px;" title="删除" class="btn btn-blue" onclick=confirm_report_delete(' + row['id'] + ',"' + row['name'] + '")>删除</button>';
                }
            }],
            page: 1,
            rowNum: 10,
            sortname: 'id',
            sortorder: 'desc',
            viewrecords: true,
            multiselect: is_administrator,
            width: 1200,
            height: mainpanelHeight,
            autowidth: true,
            gridComplete: function() {
                var gridData = $(this).jqGrid('getRowData');
                jqgrid_page($(this), gridData);
                jqgrid_resize($(this));
                report_result_init = true;
            }
        });
    }
}

function report_delete(report_id) {
    $.post("/schedule/job/delete", {
            'id': report_id
        },
        function(data) {
            if (data.reply.is_success) {
                $('#report_table').trigger("reloadGrid");
            }
        })
};

function report_confirm_delete(report_id) {
    $.post("/schedule/report/delete", {
            'id': report_id
        },
        function(data) {
            if (data.reply.is_success) {
                $('#report_all_table').trigger("reloadGrid");
            }
        })
};

function job_delete(report_id, report_name) {
    msg = '确认删除报表?';
    SelfConfirm(msg, report_delete, report_id, "删除提示");
}

function confirm_report_load(report_id) {
    window.location.href = '/schedule/report/download?id=' + report_id;
}

function confirm_report_delete(report_id, report_name) {
    msg = '确认删除报表?';
    SelfConfirm(msg, report_confirm_delete, report_id, "删除提示");
}
</script>
<div class="panel panel-f-style">
    <div class="panel-body">
        <div class="title-tag">
            <h3>统计报表</h3>
            <p>统计报表管理</p>
            <div style="height: 25px"></div>
            <ul>
                <li class="active" tid="#job_list">报表任务</li>
                <li tid="#job_result">报表结果</li>
            </ul>
        </div>
        <div id="job_list">
            <div class="grid-tools">
                <div class="grid-operate pull-left">
                    <button id="report_controller" ng-controller="ReportModalCtrl" ng-hide="true"></button>
                    <button type="button" class="btn btn-blue" id="report_create_button" ng-controller="ReportModalCtrl">创建任务</button>
                </div>
            </div>
            <div class="grid-body" id="div_report_table">
                <table id="report_table"></table>
            </div>
        </div>
        <div id="job_result" style="display: none;">
            <div class="grid-tools">
            </div>
            <div class="grid-body" id="div_report_all_table">
                <table id="report_all_table"></table>
            </div>
        </div>
    </div>
</div>
<!-- 
<div class="panel panel-default">
    <div class="panel-body">
        <button id="report_controller" ng-controller="ReportModalCtrl" ng-hide="true"></button>
        <input type="button" value="创建报表任务" class="button button_gray" id="report_create_button" ng-controller="ReportModalCtrl"/>
        <ul class="map_tab report_map">
            <li class="active" id="tab1"><a>任务列表</a></li>
            <li id="tab2"><a>报表结果</a></li>
        </ul>
        <div id="t1">
            <div id="div_report_table">
                <table id="report_table"></table>
            </div>
            <div class="item_toolsbar"></div>
        </div>
        <div id="t2">
            <div id="div_report_all_table">
                <table id="report_all_table"></table>
            </div>
            <div class="item_toolsbar"></div>
        </div>
    </div>
</div> -->
